#include "logon.h"
#include "ui_logon.h"

#include <QPushButton>
#include <QDebug>
#include <QFont>
#include <QMessageBox>

//sql的依赖
#include "sqlutil.h"


Logon::Logon(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Logon)
{
    ui->setupUi(this);
    initUI();
    initConnect();
}

Logon::~Logon()
{
    delete ui;
}

void Logon::initUI()
{
    //设置窗口图标
    setWindowIcon(QIcon(":/logon/resources/注册.png"));
    //设置窗口名字
    setWindowTitle("注册");
    QFont font("黑体",-1,-1,true);
    ui->confirmRegistration->setFont(font);
    ui->deregistration->setFont(font);
    //确认按钮图标
    ui->confirmRegistration->setIcon(QIcon(":/logon/resources/yes (1).png"));
    //取消按钮图标
    ui->deregistration->setIcon(QIcon(":/logon/resources/undo.png"));
}

void Logon::initConnect()
{
    connect(ui->confirmRegistration,&QPushButton::clicked,[=]{
        qDebug()<<"注册中...";
        QString userName = ui->userName->text();
        QString pass = ui->passWord->text();
        QString againPass = ui->againPass->text();

        if(pass.compare(againPass)==0){

            QSqlQuery query;
            QString sql = "SELECT user FROM user where user=:user";
            query.prepare(sql);
            query.bindValue(":user", userName);
            query.exec();
            if(query.next()){
                //用户存在
                //拟态窗口提示用户
                QMessageBox::information(this, "提示", "用户已存在",QMessageBox::Ok);
            }else{
                QString sql = "INSERT INTO user (user, pass) VALUES (:user,:pass)";
                query.prepare(sql);
                query.bindValue(":user", userName);
                query.bindValue(":pass", pass);
                bool isSuccess = query.exec();
                if(isSuccess){
                    qDebug()<<"注册成功！";
                    int i = QMessageBox::information(this, "提示", "注册成功",QMessageBox::Ok);
                    if(i==QMessageBox::Ok){
                        this->hide();
                    }
                }else{
                    qDebug()<<"注册失败！";
                }
            }
        }else{
            qDebug()<<"注册失败！";
        }
    });

    connect(ui->deregistration,&QPushButton::clicked,[=]{
        this->hide();
    });
}
